\b0 for both a Sound object and an NXImage object in the same file. However, I always get this warning:\
\
\f2\fs24\fc0 warning: multiple declarations for method `setName:'
\f0\fs28 \
\
at compilation time. In addition, occasionally the wrong method is called at run-time. How can I get rid of the warning and make sure the correct method is always used?\
\
A: The problem is caused by the fact that NXImage's
\b setName:
\b0 method returns a BOOLEAN, while Sound's
\b setName:
\b0 returns an
\b id
\b0 . You can avoid the problem by using static typing, instead of declaring the objects to be of type
\b id
\b0 . Static typing enables the compiler to do better type checking. The following code snippet shows how to do it:\
\
\pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f2\fs24\fc0 Sound *mySound; /* not: id mySound */\
NXImage *myImage; /* not: id myImage */\
\
mySound = [[Sound alloc] init];\
myImage = [[NXImage alloc] init];\
[mySound setName:"The Beatles"];\
[myImage setName:"The Stars"];\
\
\pard\tx740\tx1500\tx2260\tx3020\tx3780\tx4520\tx5280\tx6040\tx6800\tx7560\f0\fs28\fc0 If static typing is not possible, you can use type-casting instead:\
\f1\fs24 \
\pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f2\fc0 id mySound, myImage;\
\
mySound = [[Sound alloc] init];\
myImage = [[NXImage alloc] init];\
[(Sound *)mySound setName:"The Beatles"];\
[(NXImage *)myImage setName:"The Stars"];\
\f1\i \
\f2\i0
\f0\fs28 \
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 Note that to prevent similar problems when using duplicate method names for different classes, you should keep their argument types and return value types identical, unless you plan on using static typing to differentiate them.\
\
\fc0 Why does the compiler not do better type checking by default? The reason is that Objective-C uses dynamic binding (also known as "run-time binding") to give you greater flexibility in deciding which object will be sent a given message. Dynamic binding means that the class of the message's recipient is determined by the Objective-C run-time system rather than by the compiler. This facility lets you send the same message to any of several different classes of object (each declared as type
\b id
\b0 ), depending on the current state of the application.
\pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0 A good example of the potential of dynamic binding is InterfaceBuilder, which
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 can
\pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0 manipulate many different
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 types of
The compiler doesn't care if several different classes use exactly the same method name, because the class of the recipient won't be determined anyway until run-time; and as long as the class implements the method, all is well. In your problematic case, however, the compiler can't figure out which of two similar method names is being invoked by your code, and it may decide on the wrong name. \
\
For more on dynamic binding and Objective-C, see NeXTanswer objc.342.\